package com.example.apidocserver.interfaces.facade;

import com.example.apidocserver.application.event.DocLoadEvent;
import com.example.apidocserver.application.event.YuqueEvent;
import com.example.apidocserver.infrastructure.util.file.LoadUtils;
import com.example.apidocserver.infrastructure.util.result.ParamException;
import com.example.apidocserver.infrastructure.util.result.ResultData;
import com.example.apidocserver.infrastructure.util.result.ResultUtils;
import com.example.apidocserver.infrastructure.util.word.WordParagraphUtil;
import com.example.apidocserver.interfaces.assembler.CommonAssembler;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

import static com.example.apidocserver.infrastructure.util.file.LoadUtils.getDownEntity;
import static com.example.apidocserver.interfaces.assembler.CommonAssembler.checkInt;

/**
 * 空间内管理接口
 *
 * @author xyqlg
 */
@RestController
@RequestMapping("file")
public class FileController {
    @Resource
    private DocLoadEvent docLoadEvent;

    @Resource
    private YuqueEvent yuqueEvent;

    /**
     * 上传talend导出json文件上传文档信息
     *
     * @param file    文档信息
     * @param spaceId 空间id
     * @param docId   文件id
     * @return 标签返回对象
     * @throws IOException io
     */
    @PostMapping(value = "/updateJson/{spaceId}/{docId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultData uploadJsonFile(@RequestParam("file") MultipartFile file, @PathVariable("spaceId") Integer spaceId,
                                     @PathVariable("docId") Integer docId) throws IOException {
        CommonAssembler.checkInt(spaceId);
        CommonAssembler.checkInt(docId);
        LoadUtils.checkFileName(file, "json");

        docLoadEvent.inputMenuApi(file, spaceId, docId);

        return ResultUtils.success();
    }

    /**
     * 下载world单个接口文档
     *
     * @param apiId 接口id
     * @param envId 环境id
     * @return 标准返回对象
     */
    @GetMapping(value = "download/apiInfoOne/{apiId}/{envId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> downApiInfoById(@PathVariable("apiId") Integer apiId,
                                                  @PathVariable("envId") Integer envId) throws IOException {
        checkInt(apiId);
        checkInt(envId);
        XWPFDocument document = new XWPFDocument();
        try {
            // 在这里添加你需要生成的Word文档内容
            String filename = docLoadEvent.downApiInfoOneById(document, apiId, envId);
            return getDownEntity(document, filename);

        } catch (ParamException paramException) {
            document.close();
            throw paramException;
        }
    }

    /**
     * 生成markdown单个接口字符串
     *
     * @param apiId        接口id
     * @param envId        环境id
     * @param startTitleNo 标题开始号
     * @return 标准返回对象
     */
    @GetMapping("md/apiInfoOne/{apiId}/{envId}/{startTitleNo}")
    public ResultData copyMdApiInfoById(@PathVariable("apiId") Integer apiId, @PathVariable("envId") Integer envId,
                                        @PathVariable("startTitleNo") Integer startTitleNo) {
        checkInt(apiId);
        checkInt(envId);
        return ResultUtils.success(docLoadEvent.generateMdApiById(apiId, envId, startTitleNo));
    }

    /**
     * 生成markdown单个接口字符串
     *
     * @param docId        文档id
     * @param envId        环境id
     * @param startTitleNo 标题开始号
     * @return 标准返回对象
     */
    @GetMapping("md/doc/{docId}/{envId}/{startTitleNo}")
    public ResultData copyMdDocById(@PathVariable("docId") Integer docId, @PathVariable("envId") Integer envId,
                                    @PathVariable("startTitleNo") Integer startTitleNo) {
        checkInt(docId);
        checkInt(envId);
        return ResultUtils.success(docLoadEvent.generateMdDocById(docId, envId, startTitleNo));
    }

    /**
     * 生成语雀单个接口字符串请求参数
     *
     * @param apiId        接口id
     * @param envId        环境id
     * @param startTitleNo 标题开始号
     * @return 标准返回对象
     */
    @GetMapping(value = "yuque/apiInfoOne/{apiId}/{envId}/{startTitleNo}")
    public ResultData copyYuqueApiInfoById(@PathVariable("apiId") Integer apiId, @PathVariable("envId") Integer envId,
                                           @PathVariable("startTitleNo") Integer startTitleNo) {
        checkInt(apiId);
        checkInt(envId);
        String str = docLoadEvent.generateYuqueApiInfoById(apiId, envId, startTitleNo);
        yuqueEvent.send(str);
        return ResultUtils.success(str);
    }

    /**
     * 生成语雀单个接口字符串请求参数
     *
     * @param docId        文档id
     * @param envId        环境id
     * @param startTitleNo 标题开始号
     * @return 标准返回对象
     */
    @GetMapping(value = "yuque/doc/{docId}/{envId}/{startTitleNo}")
    public ResultData copyYuqueDocById(@PathVariable("docId") Integer docId, @PathVariable("envId") Integer envId,
                                       @PathVariable("startTitleNo") Integer startTitleNo) {
        checkInt(docId);
        checkInt(envId);
        String str = docLoadEvent.generateYuqueDocById(docId, envId, startTitleNo);
        yuqueEvent.send(str);
        return ResultUtils.success(str);
    }
}
